const express = require('express')
const router = express.Router()
const path = require('path')
const fs = require('fs')
const multer = require('multer'); //第二步
const { fstat } = require('fs');
const util_file = require('../util/util_file.js'); //加文件处理工具


//第三步，创建upload 对象  
const tmp_path = path.join(path.dirname(__dirname), 'tmp');  //文件上传的临时目录
var upload = multer({
    dest: tmp_path
});



router.use(function timeLog(req, res, next) {
    console.log('Time: ', Date.now())
    next()
})
// define the home page route
router.get('/', function (req, res) {
    var name = req.cookies.name;
    res.send('product home page:' + name)
})
// define the about route
router.get('/edit', function (req, res) {
    res.send('About product')
})

router.get('/upload', function (req, res) {

    var product_data = req.body;
    //console.log(product_data);
    res.render('files/index.html', {});
})


//第四部，在路由中加载中间件, 
router.post('/upload', upload.any(), function (req, res) {
    var files = req.files;  //req.file req.body

    //第五步： 移动文件文件处理
    var arr_file = util_file.moveFile(files);

    console.log(arr_file);


    var body = req.body;
    /*
    { user_name: '王创辉', user_age: '19' }
    */

    //console.log(files);
    //console.log(body);
    //console.log(product_data);
    //处理上传后的文件信息

    res.send('文件处理成功');
})

module.exports = router